﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.DB;
using PMPUtils.Util;

namespace PMPUtils.Configuration
{
    public class SprintOperator
    {
        /// <summary>
        /// Read the capacity for NF1
        /// </summary>
        /// <param name="sprint"></param>
        /// <returns></returns>
        public static double ReadNF1Capacity(String sprint)
        {
            String strQuery = "select NF1Capacity from sprint where id = '" + sprint + "'";
            DBOperator dbOperator = new DBOperator(DBConfigurations.LOCAL_CONNECTION_STRING);
            DataTable dtCapacity = dbOperator.Read(strQuery);

            double capacity = 0.0;
            foreach (DataRow row in dtCapacity.Rows)
            {
                if (row["NF1Capacity"] != DBNull.Value)
                {
                    capacity = Convert.ToDouble(row["NF1Capacity"]);
                }
            }

            return capacity;
        }

        /// <summary>
        /// Read capacity for NF2
        /// </summary>
        /// <param name="sprint"></param>
        /// <returns></returns>
        public static double ReadNF2Capacity(String sprint)
        {
            String strQuery = "select NF2Capacity from sprint where id = '" + sprint + "'";
            DBOperator dbOperator = new DBOperator(DBConfigurations.LOCAL_CONNECTION_STRING);
            DataTable dtCapacity = dbOperator.Read(strQuery);

            double capacity = 0.0;
            foreach (DataRow row in dtCapacity.Rows)
            {
                if (row["NF2Capacity"] != DBNull.Value)
                {
                    capacity = Convert.ToDouble(row["NF2Capacity"]);
                }
            }

            return capacity;
        }

        /// <summary>
        /// Read the sprint start date
        /// </summary>
        /// <returns></returns>
        public static DateTime ReadSprintStart(String sprint)
        {
            String strQuery = "select sprintstart from sprint where id = '" + sprint + "'";
            DBOperator dbOperator = new DBOperator(DBConfigurations.LOCAL_CONNECTION_STRING);
            DataTable dt = dbOperator.Read(strQuery);

            DateTime sprintStart = DateTime.Now;
            foreach (DataRow row in dt.Rows)
            {
                if (row["sprintStart"] != DBNull.Value)
                {
                    sprintStart = Convert.ToDateTime(row["sprintStart"]);
                    break;
                }
            }

            return sprintStart;
        }

        /// <summary>
        /// Read the offset based on the sprint start date.
        /// </summary>
        /// <param name="sprint"></param>
        /// <returns></returns>
        /// <example>Say sprint start is 2011-9-5, so it will return 1 on 2011-9-5 and 3 on 2011-9-7.</example>
        public static int ReadSprintOffset(String sprint)
        {
            DateTime sprintStartDate = ReadSprintStart(sprint);
            int dayOffset = PMPUtil.GetDayOffset(sprintStartDate);

            return dayOffset;
        }

        /// <summary>
        /// Read the offset based on sprint start date in String
        /// </summary>
        /// <param name="sprint"></param>
        /// <returns></returns>
        public static String ReadSprintOffsetString(String sprint)
        {
            int dayOffset = ReadSprintOffset(sprint);
            return "weekday" + dayOffset.ToString().PadLeft(2, '0');
        }

        /// <summary>
        /// Return the current sprint.
        /// </summary>
        /// <returns></returns>
        public static String ReadCurrentSprint()
        {
            String currentSprint = String.Empty;

            String strQuery = "select top 1 id, sprint from sprint order by iscompleted, sprint";
            DBOperator dbOperator = new DBOperator(DBConfigurations.LOCAL_CONNECTION_STRING);
            DataTable dtCurrentSprint = dbOperator.Read(strQuery);

            foreach (DataRow row in dtCurrentSprint.Rows)
            {
                if (row["id"] != DBNull.Value)
                {
                    currentSprint = row["id"].ToString();
                }
            }

            return currentSprint;
        }
    }
}
